package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Flevel {
	int flid;
	int l;
	int price;
	public Flevel(){};
	public Flevel(int flid, int l, int price) {
		this.flid = flid;
		this.l = l;
		this.price = price;
	}
	public int getFlid() {
		return flid;
	}
	public void setFlid(int flid) {
		this.flid = flid;
	}
	public int getL() {
		return l;
	}
	public void setL(int l) {
		this.l = l;
	}
	public int getPrice() {
		return price;
	}
	public void setPrice(int price) {
		this.price = price;
	}
	public boolean Create() {
		PreparedStatement sqlr = null;
		boolean result = false;

		Connection con = new ConnectDB().getConnection();
		try {
			sqlr = con
					.prepareStatement("INSERT INTO flevel( level, price) VALUES (? , ?)");
			
			sqlr.setInt(1, this.l);
			sqlr.setInt(2, this.price);
			int number = sqlr.executeUpdate();
			if (number > 0) {
				result = true;
			}
			sqlr.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	public boolean del(){
		PreparedStatement delpd = null;
		Connection con = new ConnectDB().getConnection();
		try {
			delpd = con.prepareStatement("DELETE FROM flevel WHERE lfid = ?");
			delpd.setInt(1, this.l);
			int number = delpd.executeUpdate();
			if (number > 0) {
				return true;
			}
			delpd.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return  false;
	}
	public List<Flevel> getListLevelByLid(int lid ){
		List<Flevel> result = new ArrayList<Flevel>();
		PreparedStatement sqlp = null;
		Connection connection = new ConnectDB().getConnection();
		try {
			sqlp = connection.prepareStatement("SELECT * FROM flevel where level= ? ");
			sqlp.setInt(1, this.flid);
			ResultSet rs = sqlp.executeQuery();
			while (rs.next() == true) {
				int id = rs.getInt(1);
				int l = rs.getInt(2);
				int p = rs.getInt(3);
				result.add(new Flevel(id, l, p));
			}
			sqlp.close();
			connection.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	public List<Flevel> getAll( ){
		List<Flevel> result = new ArrayList<Flevel>();
		PreparedStatement sqlp = null;
		Connection connection = new ConnectDB().getConnection();
		try {
			sqlp = connection.prepareStatement("SELECT * FROM flevel Order By level ASC");
			ResultSet rs = sqlp.executeQuery();
			while (rs.next() == true) {
				int id = rs.getInt(1);
				int l = rs.getInt(2);
				int p = rs.getInt(3);
				result.add(new Flevel(id, l, p));
			}
			sqlp.close();
			connection.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	
}
